/**
 * Created by wangliang on 2018/12/4.
 */
$(function () {
    $('.lyx_datetimepicker').datetimepicker({
        format: 'YYYY/MM/DD'
    });
})
/**
 *
 * @param alertType class类型
 * @param html 提示内容
 * @param authClose 秒
 */
function lyxAlert(alertType,html,authClose) {

    if(alertType == '' || alertType == undefined || alertType == null){
        alertType = 'alert-warning';
    }
    if(html == '' || html == undefined || html == null){
        html = '<strong>Holy guacamole!</strong> You should check in on some of those fields below.';
    }
    if(authClose == '' || authClose == undefined || authClose == null){
        authClose = false;
    }
    var alertHtml = ['<div class="alert '+alertType+ ' alert-dismissible fade show" role="alert">',
        html,
        '            <button type="button" class="close" data-dismiss="alert" aria-label="Close">',
        '                <span aria-hidden="true">&times;</span>',
        '            </button>',
        '</div>'].join("");
    $('.lyx_alert').html(alertHtml);
    if(authClose >0){
        setTimeout(function () {
            $(".alert").alert('close');
        },authClose)
    }
}
/**
 * 顶部弹出信息
 * @param msg
 */
function layuiTopMsg(msg) {
    layer.msg(msg, {
        offset: 't',
        anim: 6
    });
}
/**
 *
 */
function successAlert(msg,url) {
    if(msg == '' || msg == null || msg == undefined) {
        msg = '保存成功,点击确定返回首页';
    }
    if(url == '' || url == null || url == undefined) {
        url = 'home';
    }

    layer.alert(msg, {
        skin: 'layui-layer-lan' //样式类名
        ,closeBtn: 0
    }, function(){
        layer.closeAll();
        location.href = url;
    });
}

function ajaxLoading(type){
    if(type == true){
        layer.load(2);
    }else{
        layer.closeAll('loading');
    }
}

(function(hdWindow) {

    hdWindow.hdAjax = function(jsonParam) {
        if(jsonParam.is_loading == false ? false : true) {
            ajaxLoading(true);
        }
        var funErr = function(resultErr) {
            if(jsonParam.is_loading == false ? false : true) {
                ajaxLoading(false);
            }
            try{
                var msg = [];
                $.each(resultErr.responseJSON.errors,function (index,item) {
                    msg.push(item[0]);
                })
                if(!msg[0]){
                    msg[0] = resultErr.responseJSON.message;
                }
                layuiTopMsg(msg[0]);
            }catch (e){
                console.log(resultErr);
            }
        };

        var funSucc = function(resultData) {
            if(jsonParam.is_loading == false ? false : true) {
                ajaxLoading(false);
            }
            // 平台接口返回
            if(resultData.code != undefined && resultData.code != 'undefined') {
                jsonParam.success(resultData);
            } else {
                console.log('===>>>未知接口<<<===', option, resultData);
                jsonParam.success(resultData);
            }
        };

        var option = {
            url: '',
            type: 'get',
            data: {},
            dataType: 'json',
            timeout: 5000
        };

        $.extend(true, option, jsonParam);

        $.ajax({
            type: option.type,
            url: option.url,
            data: option.data,
            dataType: option.dataType,
            success: funSucc,
            error: funErr,
            beforeSend:function(XHR){
                console.log('------接口准备就绪，准备开始执行------');
            },
            complete:function(XHR, TS){
                console.log('------接口执行完毕-------');
            }
        });
    }

})(window);